package admin

import (
	"encoding/json"
	"go-cmdb/models"
	"go-cmdb/pkg"
	"unicode/utf8"
)

type UserController struct {
	AdminController
}

func NewUserController() *UserController {
	ctrl := &UserController{}

	return ctrl
}
func (ctrl *UserController) Index() {

	ctrl.ViewData(map[string]interface{}{
		"title": "用户管理",
	})
	return

}
func (ctrl *UserController) Page() {

	var (
		pageSize, _  = ctrl.GetUint64("size", 0)
		pageIndex, _ = ctrl.GetUint64("page", 0)
	)

	lst, count, _ := models.NewUser().GetByPage(pageSize, pageIndex)

	ctrl.OK(map[string]interface{}{
		"rows":  lst,
		"total": count,
	})
	return

}

func (ctrl *UserController) Find() {
	var (
		id, _ = ctrl.GetUint64("id", 0)
	)
	m, _ := models.NewUser().Find(id)
	ctrl.OK(m)
	return
}
func (ctrl *UserController) Add() {
	var req models.AddUserReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	c, _ := models.NewUser().CountByName(req.Name)
	if c > 0 {
		ctrl.ERROR("[UserName]不能重复")
		return
	}

	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewUser().Add(curUser.UserID, req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return
}

func (ctrl *UserController) Update() {
	var req models.UpdateUserReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curRole, _ := models.NewUser().Find(req.ID)
	if curRole.UserName != req.Name {
		c, _ := models.NewUser().CountByName(req.Name)
		if c > 0 {
			ctrl.ERROR("[Code]不能重复")
			return
		}
	}

	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewUser().Update(curUser.UserID, req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return
}

func (ctrl *UserController) Remove() {

	var req models.RemoveUserReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewUser().CStatus(0, req.ID, curUser.UserID); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}

func (ctrl *UserController) Enable() {

	var req models.EnableUserReq

	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curUser, _ := ctrl.GetAuthUser()
	if err := models.NewUser().CStatus(req.Status, req.ID, curUser.UserID); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}

func (ctrl *UserController) ChangePwd() {

	var req models.ChangePwdReq
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	if req.Pwd02 != req.Pwd01 {
		ctrl.ERROR("两次输入的密码不一致")
		return
	}
	pLen := utf8.RuneCountInString(req.Pwd01)
	if pLen < 6 || pLen > 20 {
		ctrl.ERROR("密码长度限制（mix:6,max:20）")
		return
	}

	curUser, _ := ctrl.CurUser()
	if curUser.Password != pkg.FormatPwd(req.OldPwd) {
		ctrl.ERROR("原始密码不对")
		return
	}

	if err := models.NewUser().ChangePwd(curUser.ID, pkg.FormatPwd(req.Pwd02)); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}

func (ctrl *UserController) ResetPwd() {

	var req models.RestPwdReq
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}

	if err := models.NewUser().ChangePwd(req.ID, pkg.FormatPwd(pkg.DefaultPwd)); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return

}
func (ctrl *UserController) ChangeMyUser() {
	var req models.ChangeUserInfoReq
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	curUser, _ := ctrl.CurUser()
	if curUser.UserName != req.Name {
		c, _ := models.NewUser().CountByName(req.Name)
		if c > 0 {
			ctrl.ERROR("[Code]不能重复")
			return
		}
	}

	if err := models.NewUser().ChangeUserInfo(curUser.ID, req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	ctrl.OK(nil)
	return
}
func (ctrl *UserController) MyUser() {

	m, _ := ctrl.CurUser()
	ctrl.OK(m)
	return
}
